package com.example.ribbon.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.example.auth.domain.DataNode;
import com.example.config.auth.SecurityUtil;
import com.example.config.ribbon.ServiceCaller;
import com.example.feign.UserFeignService;
import com.example.ribbon.Constant;
import com.example.ribbon.service.FriendGroupRibbonService;
import com.example.util.constant.NodeUrlConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.UriComponentsBuilder;

@Service
public class FriendGroupRibbonServiceImpl implements FriendGroupRibbonService {

    @Autowired
    private UserFeignService userFeignService;

    @Autowired
    @Lazy
    ServiceCaller serviceCaller;

    @Override
    public JSONObject getFriendGroupByUUID(String uuid) {
        String userId= SecurityUtil.getUserId();
        DataNode dataNode =userFeignService.getUserNodeByNodeNoUserId(userId, Constant.DATA_SERVER_NAME);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("uuid",uuid);
        String url = UriComponentsBuilder
                .fromUriString(NodeUrlConstant.BUS_FRIEND_GROUP_GETONE)
                .queryParams(params)
                .toUriString();
        return serviceCaller.get(dataNode.getNodeServerName(),url,JSONObject.class,new JSONObject());
    }

    @Override
    public JSONObject saveFriendGroup(String friendUid, String friendNote) {
        String userId= SecurityUtil.getUserId();
        DataNode dataNode =userFeignService.getUserNodeByNodeNoUserId(userId, Constant.DATA_SERVER_NAME);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("friendUid",friendUid);
        jsonObject.put("friendNote",friendNote);
        return serviceCaller.post(dataNode.getNodeServerName(),NodeUrlConstant.BUS_FRIEND_GROUP_SAVE,jsonObject,JSONObject.class);
    }

    @Override
    public JSONObject pageFriendGroup(Integer pageNum, Integer pageSize) {
        String userId= SecurityUtil.getUserId();
        DataNode dataNode =userFeignService.getUserNodeByNodeNoUserId(userId, Constant.DATA_SERVER_NAME);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("pageNum",pageNum.toString());
        params.add("pageSize",pageSize.toString());
        String url = UriComponentsBuilder
                .fromUriString(NodeUrlConstant.BUS_FRIEND_GROUP_PAGE)
                .queryParams(params)
                .toUriString();
        return serviceCaller.get(dataNode.getNodeServerName(),url,JSONObject.class,new JSONObject());
    }

    @Override
    public JSONObject updateFriendGroupNote(String uuid, String note) {
        String userId= SecurityUtil.getUserId();
        DataNode dataNode =userFeignService.getUserNodeByNodeNoUserId(userId, Constant.DATA_SERVER_NAME);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("uuid",uuid);
        params.add("note",note);
        String url=UriComponentsBuilder
                .fromUriString(NodeUrlConstant.BUS_FRIEND_GROUP_UPDATE)
                .queryParams(params)
                .toUriString();
        return serviceCaller.post(dataNode.getNodeServerName(),url,new JSONObject(),JSONObject.class);
    }

    @Override
    public JSONObject deleteFriendGroup(String uuid) {
        String userId= SecurityUtil.getUserId();
        DataNode dataNode =userFeignService.getUserNodeByNodeNoUserId(userId, Constant.DATA_SERVER_NAME);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("uuid",uuid);
        String url=UriComponentsBuilder
                .fromUriString(NodeUrlConstant.BUS_FRIEND_GROUP_DELETE)
                .queryParams(params)
                .toUriString();
        return serviceCaller.post(dataNode.getNodeServerName(),url,new JSONObject(),JSONObject.class);
    }

    @Override
    public JSONObject receiveFriendGroup(String uid, String friendUid) {
        //receiveFriendGroup
        DataNode dataNode =userFeignService.getUserNodeByNodeNoUserId(uid, Constant.DATA_SERVER_NAME);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
        params.add("friendUid",friendUid);
        params.add("uid",uid);
        String url=UriComponentsBuilder
                .fromUriString(NodeUrlConstant.BUS_FRIEND_GROUP_RECEIVE)
                .queryParams(params)
                .toUriString();
        return serviceCaller.post(dataNode.getNodeServerName(),url,new JSONObject(),JSONObject.class);
    }


}
